package org.jeecg.modules.database.sharding.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.database.sharding.constats.LogConstant;
import org.jeecg.modules.database.sharding.entity.ShardingSysLog;
import org.jeecg.modules.database.sharding.mapper.ShardingSysLogMapper;
import org.jeecg.modules.database.sharding.service.IShardingSysLogService;
import org.jeecg.modules.database.sharding.dict.LogTypeEnum;
import org.jeecg.modules.database.sharding.dict.OperateTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@DS("sharding")
@Slf4j
public class ShardingSysLogServiceImpl extends ServiceImpl<ShardingSysLogMapper, ShardingSysLog> implements IShardingSysLogService {

    @Resource
    private ShardingSysLogMapper shardingSysLogMapper;
    @Override
    public List<ShardingSysLog> listByDate(String datetime) {
        return shardingSysLogMapper.listByDate(datetime);
    }


    @Override
    public IPage<ShardingSysLog> queryByCondition(JSONObject jsonObject, Page<ShardingSysLog> page) throws Exception {
        LambdaQueryWrapper<ShardingSysLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(
                ShardingSysLog::getId,
                ShardingSysLog::getCreateBy,
                ShardingSysLog::getCreateTime,
                ShardingSysLog::getCostTime,
                ShardingSysLog::getIp,
                ShardingSysLog::getRequestParam,
                ShardingSysLog::getRequestType,
                ShardingSysLog::getRequestUrl,
                ShardingSysLog::getMethod,
                ShardingSysLog::getUsername,
                ShardingSysLog::getUserid,
                ShardingSysLog::getLogContent,
                ShardingSysLog::getLogType,
                ShardingSysLog::getOperateType,
                ShardingSysLog::getBelongOrgName,
                ShardingSysLog::getBelongDepartName

        );
        log.info("queryByCondition:{}", jsonObject);
        String beginDate = jsonObject.getString("beginDate");
        String endDate = jsonObject.getString("endDate");
        String keyWord = jsonObject.getString("keyWord");
        if (keyWord != null && !keyWord.isEmpty()) {
            queryWrapper.like(ShardingSysLog::getLogContent, keyWord);
        }
        if (!"".equals(beginDate) && !"".equals(endDate)) {
            queryWrapper.between(ShardingSysLog::getCreateTime,
                    new SimpleDateFormat(LogConstant.Date_FORMAT).parse(beginDate),
                    new SimpleDateFormat(LogConstant.Date_FORMAT).parse(endDate));
        } else {
            queryWrapper.between(ShardingSysLog::getCreateTime,
                    new Date(DateUtil.beginOfMonth(new Date()).getTime()),
                    new Date(DateUtil.endOfMonth(new Date()).getTime()));
        }
        queryWrapper.orderByDesc(ShardingSysLog::getCreateTime);
        Page<ShardingSysLog> shardingSysLogPage = shardingSysLogMapper.selectPage(page, queryWrapper);
        // get the record
        List<ShardingSysLog> records = shardingSysLogPage.getRecords();
        // change log type and operate type to its dict value
        for (ShardingSysLog record : records) {
            // get log and operate type dict value from dict enum class
            record.setLogType_dictText(LogTypeEnum.getDictValue(record.getLogType()));
            record.setOperateType_dictText(OperateTypeEnum.getDictValue(record.getOperateType()));

        }
        shardingSysLogPage.setRecords(records);
        return shardingSysLogPage;
    }
}
